/**
 * Created by LMN on 2017/1/11.
 */
//确认窗
var confirmed = null,uncertainty = null;
function confirmOut(msg, confirm, cancel){
    var mod = $('#my-confirm');
    $('#my-confirm-content').text(msg);
    mod.modal();
    mod.find('button[name="focus"]').focus();
    if(confirm) confirmed = function () {
        mod.modal('hide');
        confirm();
    };
    else confirmed = function () {};
    if(cancel) uncertainty = function () {
        mod.modal('hide');
        cancel();
    };
    else uncertainty = function () {};
}
function erroralert(msg){
    var mod = $('#my-alert');
    var loginA = $('#my-login-a');
    loginA.hide();
    $('#my-alert-title').text('错误！');
    $('#my-alert-content').text(msg);
    if(msg=='未登录'){
        loginA.show();
    }
    mod.modal();
    mod.find('button[name="focus"]').focus();
}
function errordelay(msg,callback){
    var mod = $('#my-alert');
    $('#my-alert-title').text('错误！');
    $('#my-alert-content').text(msg);
    mod.modal();
    mod.find('button[name="focus"]').focus();
    setTimeout(function(){
        mod.modal('hide');
        if(callback) callback();
    },1000);
}
function messagedelay(msg,callback){
    var mod = $('#my-alert');
    var loginA = $('#my-login-a');
    loginA.hide();
    $('#my-alert-title').text('消息:');
    $('#my-alert-content').text(msg);
    mod.modal();
    mod.find('button[name="focus"]').focus();
    setTimeout(function(){
        mod.modal('hide');
        if(callback) callback();
    },1000);
}
function messagealert(msg){
    var mod = $('#my-alert');
    var loginA = $('#my-login-a');
    loginA.hide();
    $('#my-alert-title').text('消息:');
    $('#my-alert-content').text(msg);
    mod.modal();
    mod.find('button[name="focus"]').focus();
}

function numberToStr(num, long){
    var str = '000'+num;
    return str.substring(str.length - long, str.length);
}
function dateFormatter(value) {
    var t = new Date(value);
    if(!t.getTime()) return '-';
    else {
        return t.getFullYear()+'-'+
            numberToStr(t.getMonth()+1,2)+'-'+
            numberToStr(t.getDate(),2)+' '+
            numberToStr(t.getHours(),2)+':'+
            numberToStr(t.getMinutes(),2)+':'+
            numberToStr(t.getSeconds(),2)
    }
}
function byteToStr(size) {
    if(size/1000<1) return size.toFixed(2) + 'B';
    size = size/1000;
    if(size/1000<1) return size.toFixed(2) + 'KB';
    size = size/1000;
    if(size/1000<1) return size.toFixed(2) + 'MB';
    size = size/1000;
    if(size/1000<1) return size.toFixed(2) + 'GB';
    size = size/1000;
    if(size/1000<1) return size.toFixed(2) + 'TB';
    size = size/1000;
    if(size/1000<1) return size.toFixed(2) + 'PB';
}
function torrentState(state) {
    if(state === 0) return '下载中';
    if(state === 1) return '暂停';
    if(state === 2) return '完成';
    if(state === 3) return '已删除';
}
function makeTreeNodes(node){
    var _node = [],_dirs = {};
    if(!node.paths) return _node;
    for(var i=0,l=node.paths.length;i<l;i++){
        var paths = node.paths[i];
        if(paths.length>1){
            if(!_dirs.hasOwnProperty(paths[0])){
                _dirs[paths[0]] = _node.length;
                _node.push({
                    text: paths[0],
                    selectable: true,
                    selectedIcon: 'glyphicon glyphicon-ok',
                    paths: [paths.splice(1)],
                    nodes:[]
                })
            }else{
                _node[_dirs[paths[0]]].paths.push(paths.splice(1));
            }
        }else{
            _node.push({
                text: paths[0],
                selectable: true,
                selectedIcon: 'glyphicon glyphicon-ok'
            });
        }
    }
    for(var j=0,len=_node.length;j<len;j++){
        if(_node[j].paths) {
            _node[j].nodes = makeTreeNodes(_node[j]);
            delete _node[j].paths;
        }
    }
    return _node;
}
function mkTreeByTorrentPath(list){
    var root = {paths:[]};
    for(var i=0,l=list.length;i<l;i++){
        root.paths.push(list[i].path.replace(/\\/g,'/').split('/'));
    }
    return makeTreeNodes(root);
}

//按照文件名判断文件类型
function fileType(name) {
    var suffix = name.split('.');
    suffix = suffix[suffix.length-1].toUpperCase();
    if(suffix === 'JPG'||suffix === 'PNG'||suffix === 'GIF'){
        return 'img'
    }else if(suffix === 'AVI'||suffix === 'MP4'||suffix === 'MKV'
        ||suffix === 'WMA'||suffix === 'RMVB'||suffix === 'RM'){
        return 'video'
    }else{
        return 'unknown'
    }
}

//时间转秒
function timeToSec (str) {
    var times = str.split(':');
    if(times.length !== 3) return null;
    return times[0]*3600 + times[1]*60 + times[2]*1;
};